{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-20T07:21:51.466800Z",
     "start_time": "2025-05-20T07:21:51.451985Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from operator import truediv\n",
    "\n",
    "{\n",
    " \"cells\": [\n",
    "  {\n",
    "   \"cell_type\": \"code\",\n",
    "   \"id\": \"initial_id\",\n",
    "   \"metadata\": {\n",
    "    \"ExecuteTime\": {\n",
    "     \"end_time\": \"2025-05-09T00:37:58.649618Z\",\n",
    "     \"start_time\": \"2025-05-09T00:37:58.461547Z\"\n",
    "    }\n",
    "   },\n",
    "   \"source\": [\n",
    "    \"import numpy as np\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 定义激活函数和其导数\\n\",\n",
    "    \"def sigmoid(x):\\n\",\n",
    "    \"    return 1 / (1 + np.exp(-x))\\n\",\n",
    "    \"\\n\",\n",
    "    \"def sigmoid_derivative(x):\\n\",\n",
    "    \"    return x * (1 - x)\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 输入数据\\n\",\n",
    "    \"inputs = np.array([[0, 0],\\n\",\n",
    "    \"                    [0, 1],\\n\",\n",
    "    \"                    [1, 0],\\n\",\n",
    "    \"                    [1, 1]])\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 输出数据\\n\",\n",
    "    \"expected_output = np.array([[0], [1], [1], [0]])\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 设置随机数种子\\n\",\n",
    "    \"np.random.seed(0)\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 初始化权重和偏置\\n\",\n",
    "    \"weights = np.random.rand(2, 1)\\n\",\n",
    "    \"biases = np.random.rand(1)\\n\",\n",
    "    \"learning_rate = 0.1\\n\",\n",
    "    \"epochs = 10000\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 训练过程\\n\",\n",
    "    \"for epoch in range(epochs):\\n\",\n",
    "    \"    # 前向传播\\n\",\n",
    "    \"    inputs_dot_weights = np.dot(inputs, weights) + biases\\n\",\n",
    "    \"    predictions = sigmoid(inputs_dot_weights)\\n\",\n",
    "    \"\\n\",\n",
    "    \"    # 计算误差\\n\",\n",
    "    \"    error = expected_output - predictions\\n\",\n",
    "    \"    loss = error ** 2\\n\",\n",
    "    \"\\n\",\n",
    "    \"    # 反向传播\\n\",\n",
    "    \"    d_predicted_output = error * sigmoid_derivative(predictions)\\n\",\n",
    "    \"\\n\",\n",
    "    \"    # 更新权重和偏置\\n\",\n",
    "    \"    inputs_transposed = inputs.T\\n\",\n",
    "    \"    weights += np.dot(inputs_transposed, d_predicted_output) * learning_rate\\n\",\n",
    "    \"    biases += np.sum(d_predicted_output, axis=0) * learning_rate\\n\",\n",
    "    \"\\n\",\n",
    "    \"    # 打印损失\\n\",\n",
    "    \"    if epoch % 1000 == 0:\\n\",\n",
    "    \"        print(f\\\"Epoch {epoch}, Loss: {np.mean(loss)}\\\")\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 测试网络\\n\",\n",
    "    \"print(\\\"Final predictions:\\\")\\n\",\n",
    "    \"print(predictions)\"\n",
    "   ],\n",
    "   \"outputs\": [\n",
    "    {\n",
    "     \"name\": \"stdout\",\n",
    "     \"output_type\": \"stream\",\n",
    "     \"text\": [\n",
    "      \"Epoch 0, Loss: 0.31751855203891893\\n\",\n",
    "      \"Epoch 1000, Loss: 0.25000078957232946\\n\",\n",
    "      \"Epoch 2000, Loss: 0.2500000003028166\\n\",\n",
    "      \"Epoch 3000, Loss: 0.2500000000001167\\n\",\n",
    "      \"Epoch 4000, Loss: 0.25000000000000006\\n\",\n",
    "      \"Epoch 5000, Loss: 0.25\\n\",\n",
    "      \"Epoch 6000, Loss: 0.25\\n\",\n",
    "      \"Epoch 7000, Loss: 0.25\\n\",\n",
    "      \"Epoch 8000, Loss: 0.25\\n\",\n",
    "      \"Epoch 9000, Loss: 0.25\\n\",\n",
    "      \"Final predictions:\\n\",\n",
    "      \"[[0.5]\\n\",\n",
    "      \" [0.5]\\n\",\n",
    "      \" [0.5]\\n\",\n",
    "      \" [0.5]]\\n\"\n",
    "     ]\n",
    "    }\n",
    "   ],\n",
    "   \"execution_count\": 1\n",
    "  }\n",
    " ],\n",
    " \"metadata\": {\n",
    "  \"kernelspec\": {\n",
    "   \"display_name\": \"Python 3\",\n",
    "   \"language\": \"python\",\n",
    "   \"name\": \"python3\"\n",
    "  },\n",
    "  \"language_info\": {\n",
    "   \"codemirror_mode\": {\n",
    "    \"name\": \"ipython\",\n",
    "    \"version\": 2\n",
    "   },\n",
    "   \"file_extension\": \".py\",\n",
    "   \"mimetype\": \"text/x-python\",\n",
    "   \"name\": \"python\",\n",
    "   \"nbconvert_exporter\": \"python\",\n",
    "   \"pygments_lexer\": \"ipython2\",\n",
    "   \"version\": \"2.7.6\"\n",
    "  }\n",
    " },\n",
    " \"nbformat\": 4,\n",
    " \"nbformat_minor\": 5\n",
    "}\n"
   ],
   "id": "7e24f23aa526d5a0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'cells': [{'cell_type': 'code',\n",
       "   'id': 'initial_id',\n",
       "   'metadata': {'ExecuteTime': {'end_time': '2025-05-09T00:37:58.649618Z',\n",
       "     'start_time': '2025-05-09T00:37:58.461547Z'}},\n",
       "   'source': ['import numpy as np\\n',\n",
       "    '\\n',\n",
       "    '# 定义激活函数和其导数\\n',\n",
       "    'def sigmoid(x):\\n',\n",
       "    '    return 1 / (1 + np.exp(-x))\\n',\n",
       "    '\\n',\n",
       "    'def sigmoid_derivative(x):\\n',\n",
       "    '    return x * (1 - x)\\n',\n",
       "    '\\n',\n",
       "    '# 输入数据\\n',\n",
       "    'inputs = np.array([[0, 0],\\n',\n",
       "    '                    [0, 1],\\n',\n",
       "    '                    [1, 0],\\n',\n",
       "    '                    [1, 1]])\\n',\n",
       "    '\\n',\n",
       "    '# 输出数据\\n',\n",
       "    'expected_output = np.array([[0], [1], [1], [0]])\\n',\n",
       "    '\\n',\n",
       "    '# 设置随机数种子\\n',\n",
       "    'np.random.seed(0)\\n',\n",
       "    '\\n',\n",
       "    '# 初始化权重和偏置\\n',\n",
       "    'weights = np.random.rand(2, 1)\\n',\n",
       "    'biases = np.random.rand(1)\\n',\n",
       "    'learning_rate = 0.1\\n',\n",
       "    'epochs = 10000\\n',\n",
       "    '\\n',\n",
       "    '# 训练过程\\n',\n",
       "    'for epoch in range(epochs):\\n',\n",
       "    '    # 前向传播\\n',\n",
       "    '    inputs_dot_weights = np.dot(inputs, weights) + biases\\n',\n",
       "    '    predictions = sigmoid(inputs_dot_weights)\\n',\n",
       "    '\\n',\n",
       "    '    # 计算误差\\n',\n",
       "    '    error = expected_output - predictions\\n',\n",
       "    '    loss = error ** 2\\n',\n",
       "    '\\n',\n",
       "    '    # 反向传播\\n',\n",
       "    '    d_predicted_output = error * sigmoid_derivative(predictions)\\n',\n",
       "    '\\n',\n",
       "    '    # 更新权重和偏置\\n',\n",
       "    '    inputs_transposed = inputs.T\\n',\n",
       "    '    weights += np.dot(inputs_transposed, d_predicted_output) * learning_rate\\n',\n",
       "    '    biases += np.sum(d_predicted_output, axis=0) * learning_rate\\n',\n",
       "    '\\n',\n",
       "    '    # 打印损失\\n',\n",
       "    '    if epoch % 1000 == 0:\\n',\n",
       "    '        print(f\"Epoch {epoch}, Loss: {np.mean(loss)}\")\\n',\n",
       "    '\\n',\n",
       "    '# 测试网络\\n',\n",
       "    'print(\"Final predictions:\")\\n',\n",
       "    'print(predictions)'],\n",
       "   'outputs': [{'name': 'stdout',\n",
       "     'output_type': 'stream',\n",
       "     'text': ['Epoch 0, Loss: 0.31751855203891893\\n',\n",
       "      'Epoch 1000, Loss: 0.25000078957232946\\n',\n",
       "      'Epoch 2000, Loss: 0.2500000003028166\\n',\n",
       "      'Epoch 3000, Loss: 0.2500000000001167\\n',\n",
       "      'Epoch 4000, Loss: 0.25000000000000006\\n',\n",
       "      'Epoch 5000, Loss: 0.25\\n',\n",
       "      'Epoch 6000, Loss: 0.25\\n',\n",
       "      'Epoch 7000, Loss: 0.25\\n',\n",
       "      'Epoch 8000, Loss: 0.25\\n',\n",
       "      'Epoch 9000, Loss: 0.25\\n',\n",
       "      'Final predictions:\\n',\n",
       "      '[[0.5]\\n',\n",
       "      ' [0.5]\\n',\n",
       "      ' [0.5]\\n',\n",
       "      ' [0.5]]\\n']}],\n",
       "   'execution_count': 1}],\n",
       " 'metadata': {'kernelspec': {'display_name': 'Python 3',\n",
       "   'language': 'python',\n",
       "   'name': 'python3'},\n",
       "  'language_info': {'codemirror_mode': {'name': 'ipython', 'version': 2},\n",
       "   'file_extension': '.py',\n",
       "   'mimetype': 'text/x-python',\n",
       "   'name': 'python',\n",
       "   'nbconvert_exporter': 'python',\n",
       "   'pygments_lexer': 'ipython2',\n",
       "   'version': '2.7.6'}},\n",
       " 'nbformat': 4,\n",
       " 'nbformat_minor': 5}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
